package com.beiding.hotcode;

import java.io.File;

/*
    将Java文件分组,并整体检测,使用
 */
class JavaMonitor {

    //执行的java文件
    File javaFile;

    //所属分组
    MonitorGroup monitorGroup;

    ClassFileHolder classFileHolder;

    MyCompiler myCompiler;

    String classname;

    JavaMonitor(MyFactory myFactory, String classname) {
        this.classname = classname;
        classFileHolder = new ClassFileHolder(FileUtils.createClassFile(classname));
        javaFile = new File(myFactory.hotCodeConfig.getSrcRoot(), classname.replace(".", File.separator) + ".java");
        myCompiler = new MyCompiler(FileUtils.classpathRootString, myFactory.hotCodeConfig.getEncoding(), FileUtils.classpathRootString);
        myCompiler.changeJavaFiles(javaFile);
    }

    //执行器
    private interface Executor {

        boolean checkChange();

        Class getTheClass();

        void reloadClass();
    }

    //内部执行
    private Executor selfExe = new Executor() {
        @Override
        public boolean checkChange() {
            return doCheckChange();
        }

        @Override
        public Class getTheClass() {
            return classFileHolder.checkReloadAndGet();
        }

        @Override
        public void reloadClass() {
            doReloadClass();
        }
    };

    private Executor groupExe = new Executor() {
        @Override
        public boolean checkChange() {
            return monitorGroup.checkChange();
        }

        @Override
        public Class getTheClass() {
            return monitorGroup.get(classname);
        }

        @Override
        public void reloadClass() {
            monitorGroup.reloadClass();
        }
    };

    private Executor currentExe = selfExe;

    synchronized void setMonitorGroup(MonitorGroup monitorGroup) {
        this.monitorGroup = monitorGroup;
        if (this.monitorGroup == null) {
            currentExe = selfExe;
        } else {
            currentExe = groupExe;
        }
    }

    private boolean javaChange = false;

    private boolean change = false;

    private long lastCheckTime = 0;

    synchronized boolean doCheckChange() {
        if (change) {
            return true;
        }

        long modified = javaFile.lastModified();

        if (lastCheckTime != modified) {
            change = true;
            javaChange = true;
            lastCheckTime = modified;
        }

        if (classpathContext != null) {
            if (classpathContext.checkChange()) {
                change = true;
            }
        }

        return change;
    }

    private ClasspathContext classpathContext;

    synchronized ClasspathContext getClasspathContext() {
        if (this.monitorGroup != null) {
            return this.monitorGroup.getClasspathContext();
        }
        return classpathContext;
    }

    //TODO 如果类归属于某个分组,他自身就不再单独持有分组
    synchronized void setClasspathContext(ClasspathContext classpathContext) {
        if (this.monitorGroup != null) {
            this.monitorGroup.setClasspathContext(classpathContext);
            return;
        }
        this.classpathContext = classpathContext;
    }

    //其他所有的操作都是由该方法进行触发的
    //检查文件是否改变
    synchronized boolean checkChange() {
        return currentExe.checkChange();
    }

    Class getTheClass() {
        return currentExe.getTheClass();
    }

    synchronized void noChange() {
        change = false;
    }

    private synchronized void doReloadClass() {

        //如果非java文件的改变
        if (javaChange) {

            long currentTimeMillis = System.currentTimeMillis();
            System.out.println(javaFile.getAbsolutePath() + "  正在编译.....");
            if (myCompiler.compile()) {
                System.out.println("编译完成   耗时:" + (System.currentTimeMillis() - currentTimeMillis) + "ms");
            } else {
                System.out.println("编译失败   请检查源码");
            }
            javaChange = false;
        }
        MyOrderedClassLoader myClassLoader;

        if (classpathContext == null) {
            myClassLoader = new MyOrderedClassLoader(MapClassLoader.EMPTY);
        } else {
            myClassLoader = new MyOrderedClassLoader(MapClassLoader.EMPTY, classpathContext.getContextClassLoader());
        }
        classFileHolder.reloadClassLoader(myClassLoader);

        //即使编译失败也要更新时间
        this.noChange();
    }

    void reloadClass() {
        this.currentExe.reloadClass();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof JavaMonitor)) {
            return false;
        }
        return this.classname.equals(((JavaMonitor) obj).classname);
    }

    @Override
    public int hashCode() {
        return classname.hashCode();
    }
}
